package ahot;

public class QuickSort {

    public static void main(String[] args) {
        int[] a = new int[]{8, 9, 2, 3, 5, 6, 7};

        Quicksourt(a, 0, a.length - 1);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

    private static void Quicksourt(int[] a, int left, int right) {
        if (left >=right) {
            return;
        }
        int i = left;
        int j = right;

        int temp;
        while (i < j) {
            while (a[left] <= a[j] && i < j) {
                j--;
            }
            while (a[left] >= a[i] && i < j) {
                i++;
            }

            if (i < j) {
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }

        }

        if (i == j) {
            temp = a[left];
            a[left] = a[j];
            a[j] = temp;
        }

        Quicksourt(a, left, j - 1);
        Quicksourt(a, j + 1, right);
    }
}
